import { RelationType } from "@/views/types";
import RelactionList from "./src/RelactionList.vue";
import RelationCreator from "./src/RelationCreator.vue";
import RelationBatchCreator from "./src/RelationBatchCreator.vue";
import RelationEditor from "./src/RelationEditor.vue";
import RelationBatchEditor from "./src/RelationBatchEditor.vue";
import { HabtmConfiguration } from "@/views/dev-types";
import { getSeed } from "@/views/seed-dev";
import ExtraEditor from "./src/ExtraEditor.vue";
import ThroughEditor from "./src/ThroughEditor.vue";
import TargetEditor from "./src/TargetEditor.vue";
export { RelationCreator, RelationBatchCreator, RelationEditor, RelationBatchEditor, RelactionList, ExtraEditor, ThroughEditor, TargetEditor }

export interface Relaction extends HabtmConfiguration {
  type?: RelationType
}

export type ExtraType = 'general'|'code'|'through'

export const cascadeOptions = {
  none: '无', // 无级联操作，无视关联数据
  setnull: '置空', // 设置为null，一对一和一对多: 关联字段置空，多对多: 删除中间表
  force: '强制', // 强制级联删除, 一对一和一对多: 删除关联数据，多对多: 此选项意义
  check: '检查' // 检查引用，如果有引用则不允许删除
}

export const relationTypes = {hasOne: '一对一', belongsTo: '多对一', hasMany: '一对多', habtm: '多对多'}

export function save(seedName: string, type: RelationType, name: string, data: HabtmConfiguration) {
  const seed = getSeed(seedName)
  switch(type) {
    case 'belongsTo':
      seed.belongsTo = seed.belongsTo || {}
      seed.belongsTo[name] = data
      break
    case 'hasOne':
      seed.hasOne = seed.hasOne || {}
      seed.hasOne[name] = data
      break
    case 'hasMany':
      seed.hasMany = seed.hasMany || {}
      seed.hasMany[name] = data
      break
    case 'habtm':
      seed.habtm = seed.habtm || {}
      seed.habtm[name] = data
      break
  }
}